﻿using Digitalmes.ApiService.Models;
using Digitalmes.Application;
using Digitalmes.Common.Exceptions;
using Digitalmes.Common.Utils;
using Digitalmes.Domain;

namespace Digitalmes.ApiService.Controllers;

/// <summary>
/// 点位
/// </summary>
[AllowAnonymous, NoApiSecurity, NoAuditLog]
public sealed class CanvasController(IWebHostEnvironment webHostEnvironment) : ApiController
{
    private readonly IWebHostEnvironment _webHostEnvironment = webHostEnvironment;
   
    /// <summary>
    /// 获取配置信息
    /// </summary>
    /// <param name="formula">配置</param>
    /// <param name="procType">工艺类型</param>
    /// <returns></returns>
    [HttpGet("config")]
    public async Task<IActionResult> Config()
    {
        var path = Path.Combine(_webHostEnvironment.WebRootPath, "canvas", "config.json");
        if (!Path.Exists(path))
        {
            Oops.ThrowBah(ErrCode.E4012);
        }

        var content = await System.IO.File.ReadAllTextAsync(path);
        var config = JsonUtils.Deserialize<CanvasConfig>(content);
        return new JsonResult(config);
    }

    /// <summary>
    /// 获取指定配方的点位信息
    /// </summary>
    /// <param name="procType">工艺类型</param>
    /// <param name="formula">配方</param>
    /// <returns></returns>
    [HttpGet("points")]
    public async Task<IActionResult> Points(string procType, string? formula)
    {
        var baseDir = Path.Combine(_webHostEnvironment.WebRootPath, "canvas", procType);
        string path;
        if (string.IsNullOrEmpty(formula))
        {
            // 配方为空，取对应工艺类型下的第一个目录下的 json 文件
            var dir1 = Directory.GetDirectories(baseDir).FirstOrDefault();
            if (dir1 == null)
            {
                Oops.ThrowBah(ErrCode.E4012);
            }

            path = Directory.GetFiles(dir1!, "*.json", SearchOption.AllDirectories).FirstOrDefault();
        }
        else
        {
            var dir2 = Path.Combine(baseDir, formula!);
            path = Directory.GetFiles(dir2, "*.json", SearchOption.TopDirectoryOnly).FirstOrDefault();
        }

        if (path == null)
        {
            Oops.ThrowBah(ErrCode.E4012);
        }

        var content = await System.IO.File.ReadAllTextAsync(path!);
        var obj = JsonUtils.Deserialize<CanvasModel>(content);
        return new JsonResult(obj);
    }

    /// <summary>
    /// 获取指定配方的图片
    /// </summary>
    /// <param name="procType">工艺类型</param>
    /// <param name="formula">配方</param>
    /// <returns></returns>
    [HttpGet("pic")]
    public IActionResult Pic(string procType, string? formula)
    {
        var baseDir = Path.Combine(_webHostEnvironment.WebRootPath, "canvas", procType);
        string path;
        if (string.IsNullOrEmpty(formula))
        {
            // 配方为空，取对应工艺类型下的第一个目录下的 png 文件
            var dir1 = Directory.GetDirectories(baseDir).FirstOrDefault();
            if (dir1 == null)
            {
                Oops.ThrowBah(ErrCode.E4012);
            }

            path = Directory.GetFiles(dir1!, "*.png", SearchOption.AllDirectories).FirstOrDefault();
        }
        else
        {
            var dir2 = Path.Combine(baseDir, formula!);
            path = Directory.GetFiles(dir2, "*.png", SearchOption.TopDirectoryOnly).FirstOrDefault();
        }

        if (path == null)
        {
            Oops.ThrowBah(ErrCode.E4012);
        }

        var imageBytes = System.IO.File.ReadAllBytes(path!);
        return File(imageBytes, "image/png");
    }
}
